package com.android.accessibility.activity;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.VideoView;

import com.android.accessibility.R;
import com.android.accessibility.base.BaseActivity;

public class VideoActivity extends BaseActivity implements Runnable, OnCompletionListener, OnPreparedListener, OnClickListener{

    private static final SimpleDateFormat sdf = new SimpleDateFormat("mm:ss", Locale.CHINA);
    private TextView mFileView;
    private VideoView mVideoView;
    private TextView mTimeView;
    private View mMaskView;
    private Uri mUri;
    private CheckBox mPlayState;
    private int mCurrentPosition = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        parseIntent();
        setContentView(R.layout.video_layout);
        mFileView = (TextView) findViewById(R.id.filename);
        mVideoView = (VideoView) findViewById(R.id.video_view);
        mPlayState = (CheckBox) findViewById(R.id.playstate);
        mPlayState.setOnClickListener(this);
        mVideoView.setOnPreparedListener(this);
        mVideoView.setOnCompletionListener(this);
        mTimeView = (TextView) findViewById(R.id.timeview);
        mMaskView = findViewById(R.id.maskview);
        mMaskView.setOnClickListener(this);
        if (mUri != null) {
            mFileView.setText(mUri.getLastPathSegment());
            mVideoView.setVideoURI(mUri);
        }
    }

    private void parseIntent() {
        Intent intent = getIntent();
        if (intent != null) {
            mUri = intent.getData();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        resume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        pause();
    }

    @Override
    public void run() {
        if (mVideoView.isPlaying()) {
            updateTime();
            mVideoView.postDelayed(this, 500);
        }
    }

    private void updateTime() {
        int cur = mVideoView.getCurrentPosition();
        int total = mVideoView.getDuration();
        mTimeView.setText(String.format("%s/%s", sdf.format(new Date(cur)), sdf.format(new Date(total))));
    }

    @Override
    public void onCompletion(MediaPlayer mp) {
        mCurrentPosition = 0;
        mPlayState.setChecked(false);
        mVideoView.pause();
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        mVideoView.start();
        mVideoView.post(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.maskview) {
            finish();
        } else if (v.getId() == R.id.playstate) {
            if (mPlayState.isChecked()) {
                resume();
            } else {
                pause();
            }
        }
    }

    private void resume() {
        mVideoView.seekTo(mCurrentPosition);
        mVideoView.start();
        mVideoView.post(this);
    }

    private void pause() {
        mVideoView.pause();
        mCurrentPosition = mVideoView.getCurrentPosition();
    }
}
